using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Info.Basic;

using EntLibEC.Biz.Basic;
using EntLibEC.Biz.Report;
using EntLibEC.Biz;

namespace EntLibEC.View.Report
{
	/// <summary>
	/// Summary description for InventoryReport.
	/// </summary>
	public partial class InventoryReport : PageBase
	{

		protected EntLibEC.View.Cmn.Stock ucStock;
		protected EntLibEC.View.Cmn.ProductType ucProductType;
		protected EntLibEC.View.Cmn.Category ucCategory;
		protected EntLibEC.View.Cmn.YNStatus uclIsConsignment;

		private decimal costSum = 0;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			base.CheckAccess((int)AppEnum.Privilege.InventoryReportSearch,true ,null);
			if ( !Page.IsPostBack)
			{
				ucStock.BindStock(false);
				ucProductType.BindProductType(true);
				ucCategory.BindCategory();
				uclIsConsignment.BindStatus(true);	//added by entlib 2006/05/24

				btnExport.Enabled = false;
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);

		}
		#endregion

		private bool checkForm()
		{
			return true;
		}
		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			if ( !checkForm())
				return;

			if ( chkSumOnly.Checked)
			{
				DataGrid1.Visible = false;
				DataGrid2.Visible = true;
				SumOnlySearch();
			}
			else
			{
				DataGrid1.Visible = true;
				DataGrid2.Visible = false;
				ClasicalSearch();
			}
		}

		private void SumOnlySearch()
		{
			Hashtable ht = new Hashtable(10);

			ht.Add("StockSysNo", ucStock.StockSysNo);
			if ( ucProductType.ProductTypeSysNo != AppConst.IntNull)
				ht.Add("ProductType", ucProductType.ProductTypeSysNo);
			
			if ( ucCategory.C3SysNo != AppConst.IntNull )
				ht.Add("Category", " c3sysno= " + ucCategory.C3SysNo);
			else if ( ucCategory.C2SysNo != AppConst.IntNull )
				ht.Add("Category", " c2sysno= " + ucCategory.C2SysNo);
			else if ( ucCategory.C1SysNo != AppConst.IntNull )
				ht.Add("Category", " c1sysno= " + ucCategory.C1SysNo);

			if (uclIsConsignment.YNSysNo != AppConst.IntNull)
			{
				ht.Add("IsConsign", uclIsConsignment.YNSysNo);
			}

			DataSet ds = ReportManager.GetInstance().GetInventorySumOnlyDs(ht);
			DataGrid2.DataSource = ConvertDsSumOnly(ds);
			DataGrid2.DataBind();

			int rows = 0;
			if ( Util.HasMoreRow(ds) )
			{
				rows = ds.Tables[0].Rows.Count;
				btnExport.Enabled = true;
			}
			Assert(lblMessage, rows.ToString() + " record(s)", 1);

		}

		private DataSet ConvertDsSumOnly(DataSet ds)
		{
			if ( !Util.HasMoreRow(ds))
				return null;

			ds.Tables[0].Columns.Add("totalAccountQtyCvt");
			ds.Tables[0].Columns.Add("totalCostCvt");
			
			foreach( DataRow dr in ds.Tables[0].Rows )
			{
				
				dr["totalAccountQtyCvt"] = Util.TrimIntNull(dr["totalAccountQty"]).ToString(AppConst.IntFormatWithGroup);

				decimal totalCost = Util.TrimDecimalNull(dr["totalCost"]);
				dr["totalCostCvt"] = (totalCost/1.17m).ToString(AppConst.DecimalFormatWithGroup);
			}
			return ds;
		}

		private void ClasicalSearch()
		{
			Hashtable ht = new Hashtable(10);

			ht.Add("StockSysNo", ucStock.StockSysNo);
			if ( ucProductType.ProductTypeSysNo != AppConst.IntNull)
				ht.Add("ProductType", ucProductType.ProductTypeSysNo);
			
			if ( ucCategory.C3SysNo != AppConst.IntNull )
				ht.Add("Category", " c3sysno= " + ucCategory.C3SysNo);
			else if ( ucCategory.C2SysNo != AppConst.IntNull )
				ht.Add("Category", " c2sysno= " + ucCategory.C2SysNo);
			else if ( ucCategory.C1SysNo != AppConst.IntNull )
				ht.Add("Category", " c1sysno= " + ucCategory.C1SysNo);

			//added by entlib 2006/05/23
			if (uclIsConsignment.YNSysNo != AppConst.IntNull)
			{
				ht.Add("IsConsign", uclIsConsignment.YNSysNo);
			}

			if ( chkZero.Checked )
				ht.Add("IsAccountQtyZero", "any value,by entlib@jacky");

			DataSet ds = ReportManager.GetInstance().GetInventoryDs(ht);
			DataGrid1.DataSource = ConvertDs(ds);
			DataGrid1.DataBind();

			int rows = 0;
			if ( Util.HasMoreRow(ds) )
			{
				rows = ds.Tables[0].Rows.Count;
				btnExport.Enabled = true;
			}
			Assert(lblMessage, rows.ToString() + " record(s)", 1);

		}

		private DataSet ConvertDs(DataSet ds)
		{
			if ( !Util.HasMoreRow(ds))
				return null;

			ds.Tables[0].Columns.Add("ProductCostSumCvt");
			
			foreach( DataRow dr in ds.Tables[0].Rows )
			{
				
				
				decimal productcostsum = Util.TrimDecimalNull(dr["productcostsum"]);

				dr["ProductCostSumCvt"] = (productcostsum/1.17m).ToString(AppConst.DecimalFormatWithGroup);

				costSum += productcostsum;

			}
			return ds;
		}

		private string ToFooterString(int x)
		{
			if ( x == 0)
				return "";
			return x.ToString(AppConst.IntFormatWithGroup);
		}
		private string ToFooterString(decimal x)
		{
			if ( x == 0)
				return "";
			return x.ToString(AppConst.DecimalFormatWithGroup);
		}

		protected void btnExport_Click(object sender, System.EventArgs e)
		{
			StockInfo oStock = StockManager.GetInstance().Load(ucStock.StockSysNo);
			if ( chkSumOnly.Checked)
			{
				Util.ToExcel(DataGrid2, "Inventory Report", oStock.StockName);
			}
			else
			{
				Util.ToExcel(DataGrid1, "Inventory Report", oStock.StockName);
			}
			
		}

		private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.Footer)
			{
				e.Item.Cells[5].Text = ToFooterString(costSum/1.17m);
			}
		}
	}
}
